package controller;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import user.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
@RequestMapping("user")
public class UserController extends BaseController{
    private BasicDataSource basicDataSource;
    //构造器方式
    /*public UserController(BasicDataSource basicDataSource) {
        this.basicDataSource = basicDataSource;
    }*/

    //setter方式
    @Autowired //自动装配
    public void setBasicDataSource(BasicDataSource basicDataSource) {
        this.basicDataSource = basicDataSource;
    }

    //登录
    @RequestMapping(value = "SignIn",method = RequestMethod.POST)
    public String signIn(@RequestParam String username,@RequestParam String password){
        String selectSql = "select * from db_user.user where username = ? and password = ?";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = basicDataSource.getConnection();
            ps = con.prepareStatement(selectSql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if (rs.next()){
                String user_username = rs.getString("username");
                String user_password = rs.getString("password");
                User user = new User(user_username,user_password);
                session.setAttribute("user",user);
                return "redirect:/user.jsp";
            }else {
                session.setAttribute("error","check your username and password!!");
                return "redirect:/index.jsp";
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
           close(rs,ps,con);
        }
    }
    //注册
    @RequestMapping(value = "SignUp",method = RequestMethod.POST)
    public String signUp(@RequestParam String username,@RequestParam String password){
        String insertSql = "insert into db_user.user values (null,?,?)";
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = basicDataSource.getConnection();
            ps = con.prepareStatement(insertSql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.executeUpdate();
            session.setAttribute("message","insert successfully!!");
            return "redirect:/index.jsp";
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            close(null,ps,con);
        }
    }
    //退出登录
    @RequestMapping(value = "SignOut",method = RequestMethod.GET)
    public String signOut(){
        session.invalidate();
        return "redirect:/index.jsp";
    }
    //关闭
    public void close(ResultSet rs,PreparedStatement ps,Connection con){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (con != null){
            try {
                con.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
